package com.swiftops.basic.sys.service.impl;

import com.github.pagehelper.PageHelper;
import com.swiftops.basic.sys.dao.SysUserMapper;
import com.swiftops.basic.sys.model.SysUser;
import com.swiftops.basic.sys.service.SysUserService;
import com.swiftops.basic.sys.vo.entity.SysUserEntityVO;
import com.swiftops.basic.sys.vo.request.SysUserRequestVO;
import com.swiftops.basic.sys.vo.response.SysUserResponseVO;
import com.swiftops.common.constant.CommonConstant;
import com.swiftops.common.constant.CommonEnum;
import com.swiftops.common.service.AbstractService;
import com.swiftops.common.core.PageResult;
import com.swiftops.common.util.IdGen;
import com.swiftops.common.util.JasyptUtils;
import com.swiftops.common.util.ListUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import java.util.Collections;
import java.util.List;

/**
 * 用户信息
 *
 * @author yangyang
 * @date 2023/09/17
 **/
@Slf4j
@Service
@Transactional
public class SysUserServiceImpl extends AbstractService<SysUser> implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 获取用户信息分页列表
     *
     * @param sysUserRequestVO 用户信息查询条件
     * @return pageResult 用户信息分页结果
     */
    @Override
    public PageResult<SysUserResponseVO> querySysUserPageList(SysUserRequestVO sysUserRequestVO) {
        //
        if (sysUserRequestVO.getPageNumber() == null) sysUserRequestVO.setPageNumber(CommonConstant.DEFAULT_PAGENUMBER);
        if (sysUserRequestVO.getPageSize() == null) sysUserRequestVO.setPageSize(CommonConstant.DEFAULT_PAGESIZE);
        //
        List<SysUserResponseVO> sysUserResponseVOList = Collections.emptyList();
        Condition condition = new Condition(SysUser.class);
        Example.Criteria criteria = condition.createCriteria();
        //
        //if (StringUtils.isNotBlank(sysUserRequestVO.getDesc())) {
        //    criteria.andLike("userName", "%" + sysUserRequestVO.getDesc() + "%");
        //}
        //删除状态
        criteria.andEqualTo("deleteFlag", CommonEnum.StateFlagEnum.STATE_FLAG_NORMAL.getCode());
        //
        int total = this.selectCountByCondition(condition);
        if (total > 0) {
            PageHelper.startPage(sysUserRequestVO.getPageNumber(), sysUserRequestVO.getPageSize());
            List<SysUser> sysUserList = this.selectByCondition(condition);
            sysUserResponseVOList = ListUtil.convertList(sysUserList, SysUserResponseVO.class);
        }
        //
        return PageResult.of(total, sysUserResponseVOList);
    }

    /**
     * 获取用户信息详细信息
     *
     * @param id 用户信息ID
     * @return 用户信息
     */
    @Override
    public SysUserEntityVO getSysUserEntityVO(String id) {
        SysUser sysUser = this.selectByPrimaryKey(id);
        if (null == sysUser) return null;

        SysUserEntityVO sysUserEntityVO = new SysUserEntityVO();
        BeanUtils.copyProperties(sysUser, sysUserEntityVO);
        return sysUserEntityVO;
    }

    /**
     * 添加用户信息
     *
     * @param sysUserEntityVO 用户信息
     */
    @Override
    public void addSysUser(SysUserEntityVO sysUserEntityVO) throws Exception {
        SysUser selectOne = this.selectOne(new SysUser(sysUserEntityVO.getUsername()));
        if (selectOne != null) throw new Exception("用户名已经存在！");
        //
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserEntityVO, sysUser);
        sysUser.setId(IdGen.next());
        sysUser.setPassword(JasyptUtils.encrypt(sysUserEntityVO.getPassword()));
        this.insertSelective(sysUser);
    }

    /**
     * 修改用户信息
     *
     * @param sysUserEntityVO 用户信息
     */
    @Override
    public void updateSysUser(SysUserEntityVO sysUserEntityVO) {
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserEntityVO, sysUser);
        this.updateByPrimaryKeySelective(sysUser);
    }

    /**
     * @param id        主键
     * @param isUseable 启用状态
     */
    @Override
    public void useableSysUser(String id, Boolean isUseable) {
        SysUser sysUser = new SysUser();
        sysUser.setId(id);
        Byte useable = isUseable ? CommonEnum.EnableStateEnum.ENABLE_STATE_YES.getCode() : CommonEnum.EnableStateEnum.ENABLE_STATE_NO.getCode();
        sysUser.setUseable(useable);
        this.updateByPrimaryKeySelective(sysUser);
    }

    /**
     * 删除用户信息
     *
     * @param id 用户信息ID
     */
    @Override
    public void deleteSysUser(String id) {
        SysUser sysUser = new SysUser();
        sysUser.setId(id);
        //sysUser.setStateFlag(CommonEnum.StateFlagEnum.STATE_FLAG_DELETE.getCode());
        //逻辑删除
        this.updateByPrimaryKeySelective(sysUser);
    }
}
